<?php
/**
 * file video.php
 * Created on 2015-3-25 下午12:41:59 
 * Create by xiweijie
 */
namespace haibao\www\business;

use \haibao\www\data\Video as VideoData;
class Video extends \haibao\www\business\Base{
    
    public static function addVideo($model){
        self::checkOperation(self::FUNC_CODE_VIDEO_ADD);
        self::checkFormData($model);
        
        $videoData = new VideoData();
        $videoData->addVideo($model);  
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_ADD, $model->toArray());
    }
    
    public static function updateVideoById($model){
        self::checkOperation(self::FUNC_CODE_VIDEO_SAVE);
        self::checkFormData($model);
        $model->LastModifyUser = self::getCurrentUser()->Id;
        $model->LastModifyTime = new \DateTime();
        $videoData = new VideoData();
        
        $videoData->updateVideoById($model);
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_SAVE, $model->toArray());
    }
    
    public static function deleteVideoById($id){
        self::checkOperation(self::FUNC_CODE_VIDEO_DEL);
        
        if(!$id){
            throw new BusinessException('视频Id不能为空',self::DATA_EMPTY_ID);
        }
        $videoData = new VideoData();
        $arrSet = array();
        $arrSet['Status']  = \haibao\cms\model\data\Video::STATUS_DELETE;
        $videoData->updateById($id,$arrSet);
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_DEL, array('Id' =>$id));
    }
    
    public static function getVideoListByFilter($query){
        $videoData = new VideoData();
        return $videoData->getAll($query);
    }
    
    public static function getVideoListByOneFilter($query){
        $videoData = new VideoData();
        return $videoData->getOne($query);
    }
    
    public static function getVideoByid($id){
        $videoData = new VideoData();
        return $videoData->getVideoById($id);
    }
    
    public static function getVideoList($filter){
    	$videoData = new VideoData();
    	return $videoData->getAll($filter);
    }
    
    public static function getVideoTagById($id){
    	$videoData = new VideoData();
    	return $videoData->getVideoTagById($id);
    }
    
    private static function checkFormData($model){
        if(empty($model->CategoryId)){
            throw new BusinessException('栏目不能为空！',self::DATA_EMPTY);
        }
        if(empty($model->Title)){
            throw new BusinessException('标题不能为空！',self::DATA_EMPTY);
        }
        if(empty($model->VideoUrl)){
            throw new BusinessException('视频链接不能为空！',self::DATA_EMPTY);
        }
        if(empty($model->PublishDate)){
            throw new BusinessException('发布时间不能为空！',self::DATA_EMPTY);
        }
        if(empty($model->VideorelTagModel)){
            throw new BusinessException('标签不能为空！',self::DATA_EMPTY);
        }
    }
}